package com.tianqingwl.gsystem.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tianqingwl.gsystem.content.mapper.ContentInfoMapper;
import com.tianqingwl.gsystem.content.pojo.ContentType;
import com.tianqingwl.gsystem.content.mapper.ContentTypeMapper;
import com.tianqingwl.gsystem.content.service.ContentInfoService;
import com.tianqingwl.gsystem.content.service.ContentTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Service
public class ContentTypeServiceImpl implements ContentTypeService {

    @Autowired
    private ContentTypeMapper contentTypeMapper;
    @Autowired
    private ContentInfoService contentInfoService;
    @Autowired
    private ContentInfoMapper contentInfoMapper;

    @Override
    public void listContentTypePage(Page<ContentType> contentTypePage, String typeName, Integer type, Long contentId) {
        if (!StringUtils.isEmpty(typeName)) {
            typeName = "%" + typeName + "%";
        }
        contentTypeMapper.listContentTypePage(contentTypePage, typeName, type, contentId);
    }

    @Override
    public ContentType getContentTypeById(Long typeId) {
        return contentTypeMapper.selectById(typeId);
    }

    @Override
    public ContentType getContentTypeByTypeAndId(Long typeId,Integer type) {
        QueryWrapper<ContentType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("typeId",typeId);
        queryWrapper.eq("type",type);
        return contentTypeMapper.selectOne(queryWrapper);
    }

    @Transactional
    @Override
    public ContentType save(ContentType contentType) {
        if (contentType == null) {
            return null;
        }

        if (contentType.getTypeId() != null) {
            //编辑
            contentType.setParentId(null);//不修改
            contentType.setType(null);
            contentTypeMapper.updateById(contentType);
            contentInfoMapper.updateContentType(contentType);
        } else {
            //新增
            if (!StringUtils.isEmpty(contentType.getTypeName())) {
                contentTypeMapper.insert(contentType);
            } else {
                return null;
            }
        }
        return contentType;
    }

    @Override
    public boolean haveContent(List<Long> contentTypeIds) {
        for (Long contentTypeId : contentTypeIds) {
            if (contentInfoService.haveContentByTypeId(contentTypeId)) {
                return true;
            }
        }
        return false;
    }

    @Transactional
    @Override
    public void deleteById(Long contentTypeId, Integer type) {
        QueryWrapper<ContentType> q = new QueryWrapper<>();
        q.eq("typeId", contentTypeId).eq("type", type);
        contentTypeMapper.delete(q);
        //删除关联
        if (type == 2) {
            contentTypeMapper.deleteContentTagRelate(contentTypeId);
        }
    }

    private void deleteByIds(Long contentTypeId, Integer type) {
        QueryWrapper<ContentType> q = new QueryWrapper<>();
        q.eq("typeId", contentTypeId).eq("type", type);
        contentTypeMapper.delete(q);
        //删除关联
        if (type == 2) {
            contentTypeMapper.deleteContentTagRelate(contentTypeId);
        }
    }

    @Transactional
    @Override
    public void deleteByIds(List<Long> contentTypeIds, Integer type) {
        for (Long contentTypeId : contentTypeIds) {
            deleteByIds(contentTypeId, type);
        }
    }

    @Override
    public boolean haveChild(List<Long> contentTypeIds) {
        for (Long contentTypeId : contentTypeIds) {
            Integer count = contentTypeMapper.selectCount(new QueryWrapper<ContentType>().eq("parentId", contentTypeId));
            if (count > 0) {
                return true;
            }
        }
        return false;
    }

    @Override
    public List<ContentType> listAllType() {
        QueryWrapper<ContentType> queryWrapper = new QueryWrapper<ContentType>().eq("type", 1);
        return contentTypeMapper.selectList(queryWrapper);
    }

    @Override
    public boolean checkNameExists(ContentType contentType) {
        QueryWrapper<ContentType> queryWrapper = new QueryWrapper<>();
        if (contentType.getTypeId() != null) {
            queryWrapper.ne("typeId", contentType.getTypeId());
        }
        queryWrapper.eq("typeName", contentType.getTypeName());
        queryWrapper.eq("type", contentType.getType());
        Integer count = contentTypeMapper.selectCount(queryWrapper);
        return count > 0;
    }

    @Override
    public void listContentTypePage(Page<ContentType> contentTypePage, Long contentId) {
        contentTypeMapper.listContentTag(contentTypePage, contentId);
    }

    @Override
    public List<Map> listHotContentTag() {
        return contentTypeMapper.listHotContentTag();
    }
}
